#import "LXActivity.h"
#import "ScreenAdapter.h"

#define SHAREBUTTON_WIDTH                       50
#define SHAREBUTTON_HEIGHT                      50
#define SHAREBUTTON_INTERVAL_WIDTH              42.5

#define SHARETITLE_WIDTH                        50
#define SHARETITLE_HEIGHT                       20
#define SHARETITLE_INTERVAL_WIDTH               42.5

#define TITLE_FONT                              [UIFont fontWithName:@"Helvetica-Bold" size:10]

@interface LXActivity ()

@property (nonatomic,strong) UIView *backGroundView;
@property (nonatomic,strong) NSString *actionTitle;
@property (nonatomic,assign) NSInteger postionIndexNumber;
@property (nonatomic,assign) BOOL isHadShareButton;
@property (nonatomic,assign) BOOL isHadCancelButton;
@property (nonatomic,assign) CGFloat LXActivityHeight;
@property (nonatomic,assign) id<LXActivityDelegate>delegate;

@end

@implementation LXActivity

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

#pragma mark - Public method

- (id)initShareButtonTitles:(NSArray *)shareButtonTitlesArray withShareButtonImagesName:(NSArray *)shareButtonImagesNameArray delegate:(id<LXActivityDelegate>)delegate
{
    self = [super init];
    if (self) {
        //初始化背景视图，添加手势
        self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2];
        
        self.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedCancel)];
        [self addGestureRecognizer:tapGesture];
        
        if (delegate) {
            self.delegate = delegate;
        }
        
        [self shareButtonTitles:shareButtonTitlesArray withShareButtonImagesName:shareButtonImagesNameArray];
        
    }
    return self;
}

- (void)showInView:(UIView *)view
{
    [[UIApplication sharedApplication].delegate.window.rootViewController.view addSubview:self];
}

#pragma mark - Praviate method

- (void)shareButtonTitles:(NSArray *)shareButtonTitlesArray withShareButtonImagesName:(NSArray *)shareButtonImagesNameArray
{
    self.isHadShareButton = NO;
    
    //初始化LXACtionView的高度为0
    self.LXActivityHeight = 0;
    
    //初始化IndexNumber为0;
    self.postionIndexNumber = 0;
    
    //生成LXActionSheetView
    self.backGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width, 0)];
    self.backGroundView.backgroundColor = [UIColor colorWithRed:0.9f green:0.9f blue:0.9f alpha:1];
    
    //给LXActionSheetView添加响应事件
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedBackGroundView)];
    [self.backGroundView addGestureRecognizer:tapGesture];
    
    [self addSubview:self.backGroundView];
    
    ScreenAdapter *sa = [ScreenAdapter getInstanse];
    
    CGFloat maxY = 0.0f;
    if (shareButtonImagesNameArray) {
        if (shareButtonImagesNameArray.count > 0) {
            self.isHadShareButton = YES;
            
            //中间按钮
            UIButton *shareButton = [self creatShareButton];
            shareButton.tag = 1;
            [shareButton addTarget:self action:@selector(didClickOnImageIndex:) forControlEvents:UIControlEventTouchUpInside];
            
            [shareButton setBackgroundImage:[UIImage imageNamed:[shareButtonImagesNameArray objectAtIndex:1]] forState:UIControlStateNormal];

            [shareButton setFrame:CGRectMake((sa.screenWidth-SHAREBUTTON_WIDTH*sa.imageScale)/2, 10*sa.imageScale, SHAREBUTTON_WIDTH*sa.imageScale, SHAREBUTTON_HEIGHT*sa.imageScale)];

            [self.backGroundView addSubview:shareButton];
            
            //左侧按钮
            shareButton = [self creatShareButton];
            shareButton.tag = 0;
            [shareButton addTarget:self action:@selector(didClickOnImageIndex:) forControlEvents:UIControlEventTouchUpInside];
            
            [shareButton setBackgroundImage:[UIImage imageNamed:[shareButtonImagesNameArray objectAtIndex:0]] forState:UIControlStateNormal];
            
            [shareButton setFrame:CGRectMake((sa.screenWidth-SHAREBUTTON_WIDTH*sa.imageScale)/2 -0.25*sa.screenWidth, 10*sa.imageScale, SHAREBUTTON_WIDTH*sa.imageScale, SHAREBUTTON_HEIGHT*sa.imageScale)];
            
            [self.backGroundView addSubview:shareButton];
            
            //右侧按钮
            shareButton = [self creatShareButton];
            shareButton.tag = 2;
            [shareButton addTarget:self action:@selector(didClickOnImageIndex:) forControlEvents:UIControlEventTouchUpInside];
            
            [shareButton setBackgroundImage:[UIImage imageNamed:[shareButtonImagesNameArray objectAtIndex:2]] forState:UIControlStateNormal];
            
            [shareButton setFrame:CGRectMake((sa.screenWidth-SHAREBUTTON_WIDTH*sa.imageScale)/2 +0.25*sa.screenWidth, 10*sa.imageScale, SHAREBUTTON_WIDTH*sa.imageScale, SHAREBUTTON_HEIGHT*sa.imageScale)];
            
            [self.backGroundView addSubview:shareButton];
            
            maxY = CGRectGetMaxY(shareButton.frame);

        }
    }
    
    if (shareButtonTitlesArray) {
        if (shareButtonTitlesArray.count > 0 && shareButtonImagesNameArray.count > 0) {
            
            UILabel *shareLabel = [self creatShareLabel];
            [shareLabel setFrame:CGRectMake((sa.screenWidth - SHARETITLE_WIDTH*sa.imageScale)/2, maxY, SHARETITLE_WIDTH*sa.imageScale, SHARETITLE_HEIGHT*sa.imageScale)];
            shareLabel.text = [shareButtonTitlesArray objectAtIndex:1];
            [self.backGroundView addSubview:shareLabel];
            
            //左侧标签
            shareLabel = [self creatShareLabel];
            [shareLabel setFrame:CGRectMake((sa.screenWidth - SHARETITLE_WIDTH*sa.imageScale)/2 -0.25*sa.screenWidth, maxY, SHARETITLE_WIDTH*sa.imageScale, SHARETITLE_HEIGHT*sa.imageScale)];
            shareLabel.text = [shareButtonTitlesArray objectAtIndex:0];
            [self.backGroundView addSubview:shareLabel];
            
            //右侧标签
            shareLabel = [self creatShareLabel];
            [shareLabel setFrame:CGRectMake((sa.screenWidth - SHARETITLE_WIDTH*sa.imageScale)/2 +0.25*sa.screenWidth, maxY, SHARETITLE_WIDTH*sa.imageScale, SHARETITLE_HEIGHT*sa.imageScale)];
            shareLabel.text = [shareButtonTitlesArray objectAtIndex:2];

            [self.backGroundView addSubview:shareLabel];
        }
    }
    
    //再次计算加入shareButtons后LXActivity的高度
    if (shareButtonImagesNameArray && shareButtonImagesNameArray.count > 0) {
        self.LXActivityHeight = (40+SHAREBUTTON_HEIGHT)*sa.imageScale;
    }
    
    [UIView animateWithDuration:0.3 animations:^{
        [self.backGroundView setFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height-self.LXActivityHeight, [UIScreen mainScreen].bounds.size.width, self.LXActivityHeight)];
    } completion:^(BOOL finished) {
    }];
}

- (UIButton *)creatShareButton
{
    UIButton *shareButton = [[UIButton alloc] init];
    return shareButton;
}

- (UILabel *)creatShareLabel
{
    UILabel *shareLabel = [[UILabel alloc] init];
    
    shareLabel.backgroundColor = [UIColor clearColor];
    shareLabel.textAlignment = NSTextAlignmentCenter;
    shareLabel.font = TITLE_FONT;
    shareLabel.textColor = [UIColor blackColor];
    return shareLabel;
}

- (void)didClickOnImageIndex:(UIButton *)button
{
    if (self.delegate) {
        if (button.tag != self.postionIndexNumber-1) {
            if ([self.delegate respondsToSelector:@selector(didClickOnImageIndex:)] == YES) {
                [self.delegate didClickOnImageIndex:(NSInteger *)button.tag];
            }
        }
        else{
            if ([self.delegate respondsToSelector:@selector(didClickOnCancelButton)] == YES){
                [self.delegate didClickOnCancelButton];
            }
        }
    }
    [self tappedCancel];
}

- (void)tappedCancel
{
    [UIView animateWithDuration:0.3 animations:^{
        [self.backGroundView setFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width, 0)];
        self.alpha = 0;
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }
    }];
}

- (void)tappedBackGroundView
{
    //
    
}

@end
